package cn.imvc.app.controllers.goods;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.GoodsAttrValue;
import cn.imvc.app.entities.GoodsClass;
import cn.imvc.app.models.GoodsAttrValueModel;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

@Service
public class GoodsAttrValueControllerImpl implements GoodsAttrValueController {

	@Autowired
	GoodsAttrValueModel goodsAttrValueModel;
	@Autowired
	GoodsModel goodsModel;
	
	public Page<GoodsAttrValue> page;
	public ViewRequest viewRequest;
	public int id;
	public int goodsId;
	public int[] ids;
	public GoodsAttrValue goodsAttrValue;
	
	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setGoodsAttrValue(GoodsAttrValue goodsAttrValue) {
		this.goodsAttrValue = goodsAttrValue;
	}

	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("goodsId_eq_string", String.valueOf(goodsId));
		viewRequest = new ViewRequest();
		viewRequest.setParams(searchMap);
		page=goodsAttrValueModel.find(viewRequest);
		return "goods-attr-value/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		goodsAttrValue = new GoodsAttrValue();
		goodsAttrValue.setGoodsId(goodsId);
		return "goods-attr-value/edit";
	}

	public String create() {
		if(goodsAttrValue.getAttrValue() != null && goodsAttrValue.getAttrValue().getId() == 0){
			goodsAttrValue.setAttrValue(null);
		}
		if(goodsAttrValueModel.save(goodsAttrValue) != null){
			return Render.json(new DialogBean("200", "创建成功","goods_attr_value", "closeCurrent"));
		}else{
			return Render.json(new DialogBean("300", "创建失败，已存在该属性","", ""));
		}
	}

	public String edit() {
		goodsAttrValue = goodsAttrValueModel.get(id);
		return "goods-attr-value/edit";
	}

	public String update() {
		if(goodsAttrValueModel.update(goodsAttrValue) != null){
			return Render.json(new DialogBean("200", "更新成功","goods_attr_value", "closeCurrent"));
		}else{
			return Render.json(new DialogBean("300", "更新失败，已存在该属性","", ""));
		}
	}

	public String destroy() {
		goodsAttrValueModel.delete(id);
		return Render.json(new DialogBean("200", "删除成功","goods_attr_value"));
	}

	public String destroyBatch() {
		goodsAttrValueModel.deleteBatch(ids);
		return Render.json(new DialogBean("200", "删除成功","goods_attr_value"));
	}

}
